{% extends 'layout/basic.html' %}
{% load static %}
{% block title %}用户登录{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg{
            color: red;
            position: absolute;
            font-size: 13px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="account">
        <div class="title">用户登录</div>
        <form id="regForm" action="{% url 'login_sms' %}" method="post" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5" style="padding-left: 0;">
                                <input id="btnSms" class="btn btn-info" type="button" value="获取验证码">
                                <span class="error-msg"></span>
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
{#                        <span class="error-msg">{{ field.errors.0 }}</span>#}
                            <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}
            <div style="float: right;">
                    <a href="{% url 'login' %}">用户名密码登录?</a>
            </div>
            <div class="row">
                <div class="col-xs-3">
                    <!--
                    用于form提交 缺点:(验证码倒计时重写)
                   <input style="margin-left: 135px;margin-top: 20px;" type="submit" class="btn btn-primary" value="注 册">
                   -->
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="登 录">
                </div>
            </div>


        </form>
    </div>
{% endblock %}

{% block js %}
    <script>
        //页面框架加载完成之后自动执行函数
        $(function () {
            bindClickBtnSms();
            bindClickSubmit();
        });
        /*
        点击提交(登录)
        */
        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                //获取form的所有的数据(包含csrf) 通过$("#regForm").serialize()
                $('.error-msg').empty();
                $.ajax({
                    url:"{% url 'login_sms' %}",
                    type:"POST",
                    data:$("#regForm").serialize(),
                    dataType: "JSON",
                    success:function (arg) {
                        if(arg.status){
                            location.href = arg.data;
                        }else{
                            $.each(arg.error,function (key,value) {
                                //这里是id下一个标签就是span标签
                                $("#id_"+key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }
        /*
        点击获取验证码的按钮绑定事件
         */
        function bindClickBtnSms() {
            $('#btnSms').click(function () {

                $('.error-msg').empty();
                // 获取用户输入的手机号
                // 找到输入框的ID，根据ID获取值
                var mobilePhone = $('#id_mobile_phone').val();
                $.ajax({
                    url:'{% url 'send_sms' %}',
                    type:'GET',
                    data:{mobile_phone:mobilePhone,tpl:'login'},
                    dataType:'JSON',
                    success:function (arg) {
                        // ajax请求发送成功之后,自动执行的函数;arg结束后端返回的值
                        if (arg.status){
                            sendSmsRemind();
                        }else{
                            //错误信息
                            $.each(arg.error,function (key,value) {
                                $("#id_"+key).next().text(value[0]);

                            })
                        }
                    }
                })
            });

        }
        function sendSmsRemind() {
            var $smsBtn = $('#btnSms');

            $smsBtn.prop('disabled',true);
            var time = 60;
            var remind = setInterval(function () {
                $smsBtn.val(time+'秒重新发送');
                time = time -1;
                if(time<1){
                    clearInterval(remind);
                    $smsBtn.val('获取验证码').prop('disabled',false);
                }

            },1000)

        }
    </script>
{% endblock %}